//    Copyright (C) Kherty.  All rights reserved.
using System;
using OpenLS.Core;

namespace OpenLS.Chart.UI
{
    class DateTimeConverter : IFormatValueConverter
    {

        public  DateTime DoubleToDateTime(double d)
        {
            {
                if (d < 60)
                {
                    return FirstDate1900.AddDays(d + 1);
                }
                return FirstDate1900.AddDays(d);
            }
        }

        ///<summary>
        /// Convert an object into a T.
        ///</summary>
        ///<param name="obj">The object to convert</param>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        public T ChangeType<T>(object obj)
        {
            if (typeof(T) == typeof(DateTime))
                return (T)(object)DoubleToDateTime((double) obj);
            throw new NotSupportedException();
        }

        public const double maxDouble = 100000;//\\
        public static double DateTimeToDouble(DateTime d)
        {
            {
                TimeSpan ts = (d - FirstDate1900);

                double result;
                /*result = ts.TotalDays + ((double) ts.Hours)/24 + ((double) ts.Minutes)/24/60 +
                                        ((double) ts.Seconds)/24/60/60 + ((double) ts.Milliseconds)/24/60/60/1000;*/
                result = ts.TotalDays;
                if (result < 60)
                    result--;
                return result;
            }

            
        }
        static readonly DateTime FirstDate1900 = new DateTime(1899, 12, 30);
    }
}